package com.gxp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gxp.mapper.AuthorityMapper;
import com.gxp.mapper.RoleMapper;
import com.gxp.mapper.UserMapper;
import com.gxp.pojo.Authority;
import com.gxp.pojo.Role;
import com.gxp.pojo.User;
import com.gxp.service.UserService;
import com.gxp.util.common.GXPConstant;
import com.gxp.util.common.GXPUtil;
import com.gxp.util.security.Des;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	
	Logger log = Logger.getLogger(UserServiceImpl.class);

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private AuthorityMapper permMapper;
	
	
	
	public User findById(User user) {
		List<Role> roleList = roleMapper.findByUser(user);
		
		for (Role role : roleList) {
			List<Authority> permList = permMapper.findByRole(role);
			role.setPermissionList(permList);
		}
		User account = userMapper.findById(user);
		account.setRoleList(roleList);
		return account;
	}
	
	public User findByUserIdForLogin(User user) {
		List<Role> roleList = roleMapper.findAllRoleByUserForLogin(user);
		
		for (Role role : roleList) {
			List<Authority> permList = permMapper.findByRole(role);
			role.setPermissionList(permList);
		}
		User account = userMapper.findById(user);
		account.setRoleList(roleList);
		return account;
	}




	@Override
	public List<User> getAllBuyers() {
		return userMapper.getUsersByRole(GXPConstant.ROLE_BUYER);
	}


	@Override
	public User getUserInfo(User findUser) {
		return userMapper.findById(findUser);
	}



	@Override
	public List<User> getAllAdminUsers() {
		List<User> users =  userMapper.getUsersByRole(GXPConstant.ROLE_ADMIN_DEFAULT);
		List<User> newList = new ArrayList<User>();
		for(User user : users) {
			newList.add(findById(user));
		}
		return newList;
	}



	@Override
	public void deleteUserById(String userId) {
		userMapper.deleteActivity(userId);
		userMapper.deleteUserRoleRelation(userId);
		userMapper.deleteByUserId(userId);
	}



	@Override
	public void addAdminUser(User user) {
		String psw = Des.encryptUserPassord(user.getUserId(), user.getPassword());
		user.setPassword(psw);
		user.setRegDate(new Date());
		userMapper.insertUser(user);
		List<Integer> roleList = user.getSelectedRoleList();
		if (GXPUtil.isNull(roleList)) {
			roleList = new ArrayList<Integer>();
		}
		roleList.add(GXPConstant.ROLE_ADMIN_DEFAULT);
		for (int roleId : roleList) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put(GXPConstant.MAPPER_KEY_USER_ID, user.getUserId());
			param.put(GXPConstant.MAPPER_KEY_ROLE_ID, roleId);
			userMapper.insUserRoleRelation(param);
		}
	}



	@Override
	public String checkUserId(String userId) {
		User user = new User();
		user.setUserId(userId);
		User result = userMapper.findById(user);
		if (result == null) {
			return null;
		} else {
			return result.getUserM();
		}
	}



	@Override
	public boolean checkPassword(User user) {
		User dbUser = userMapper.findById(user);
		if (Des.encryptUserPassord(user.getUserId(), user.getPassword()).equals(dbUser.getPassword())) {
			return true;
		}
		return false;
	}
	
	@Override
	public void insertUser(User user) {
		//insert user
		userMapper.insertUser(user);
		
		// insert user role
		List<Role> roleList = user.getRoleList();
		if (roleList != null) {
			for (Role role: roleList) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put(GXPConstant.MAPPER_KEY_USER_ID, user.getUserId());
				param.put(GXPConstant.MAPPER_KEY_ROLE_ID, role.getRoleId());
				userMapper.insUserRoleRelation(param);
			}
		}
	}

	@Override
	public User getActiveUser(String userId) {
		User user = userMapper.getActiveUser(userId);
		return user;
	}

	@Override
	public void activeUser(String userId) {
		userMapper.activeUser(userId);
	}

	@Override
	public void inactiveUser(String userId) {
		userMapper.inactiveUser(userId);
	}

	@Override
	public void updateUserInfo(User user) {
		userMapper.updateUserInfo(user);
	}

	@Override
	public void updateUserPassword(User user) {
		userMapper.updateUserPassword(user);
	}



	@Override
	public String checkUserMail(String email) {
		List<User> result = userMapper.findByMail(email);
		if (result == null || result.isEmpty()) {
			return null;
		} else {
			return result.get(0).getUserId();
		}
	}

	@Override
	public void updateUserStatus(String userId, int status) {
		userMapper.updateUserStatus(userId, status);
	}
}
